/*
自己选择的路 ,跪着也要走完。朋友们 , 虽然这个世界日益浮躁起来,只
要能够为了当时纯粹的梦想和感动坚持努力下去 , 不管其它人怎么样,我
们也能够保持自己的本色走下去。
To the world , you will be a person , but to a person , you
will be the world .                               ——AKPower
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const ll maxn = 5e4 + 100;
ll dfn[maxn], low[maxn], sccnt;
ll st[maxn], tp;
ll dfncnt;
bool in_st[maxn];
struct node
{
    ll y, next_s;
} e[maxn];
ll head[maxn], cnt;
ll S[maxn], scnt;
ll siz[maxn];
ll ou_siz[maxn];
ll in_s[maxn];
ll n, m;
void init()
{
    memset(head, -1, sizeof(head));
    cnt = 0;
}
void add(ll x, ll y)
{
    e[cnt].y = y;
    e[cnt].next_s = head[x];
    head[x] = cnt++;
    return;
}
ll find()
{
    ll out = 0;
    for (ll i = 0; i < scnt; i++)
    {
        ll u = S[i];
        for (ll j = head[u]; j != -1; j = e[j].next_s)
        {
            ll v = e[j].y;
            if (!in_s[v])
                out++;
        }
    }
    for (ll i = 0; i < scnt; i++)
    {
        in_s[S[i]] = false;
    }
    return out;
}

void tarjan(ll u)
{
    dfn[u] = low[u] = ++dfncnt;
    st[++tp] = u;
    in_st[u] = true;
    for (ll i = head[u]; i != -1; i = e[i].next_s)
    {
        ll v = e[i].y;
        if (!dfn[v])
        {
            tarjan(v);
            low[u] = min(low[v], low[u]);
        }
        else if (in_st[v])
        {
            low[u] = min(dfn[v], low[u]);
        }
    }
    ll v;
    if (dfn[u] == low[u])
    {
        ++sccnt;
        do
        {
            v = st[tp--];
            in_st[v] = false;
            S[scnt++] = v;
            siz[sccnt]++;
            in_s[v] = true;
        } while (v != u);
        ou_siz[sccnt] = find();
        scnt = 0;
    }
    return;
}

int main()
{
    IOS;
    init();
    ll x, y;
    cin >> n >> m;
    for (ll i = 1; i <= m; i++)
    {
        cin >> x >> y;
        add(x, y);
    }
    for (ll i = 1; i <= n; i++)
    {
        if (!dfn[i])
        {
            tarjan(i);
        }
    }
    ll c = 0, ansid;
    // cout << sccnt << endl;
    for (ll i = 1; i <= sccnt; i++)
    {
        // cout << ou_siz[i] << endl;
        if (!ou_siz[i])
        {
            c++;
            ansid = i;
        }
    }
    if (c == 1)
    {
        cout << siz[ansid] << endl;
    }
    else
        cout << 0 << endl;
    getchar();
    getchar();
    return 0;
}
